<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>推广活动</title>
    <link rel="stylesheet" href="layui/css/layui.css">
    <script src="layui/layui.js"></script>
    <script src="kindeditor/kindeditor-all.js"></script>
    <script src="kindeditor/plugins/code/prettify.js"></script>
    <script src="kindeditor/lang/zh-CN.js"></script>
</head>
<body>
<div class="layui-row">
    <div class="layui-col-lg10">
        <form class="layui-form" style="margin-top: 50px" name="activity_form">
            <div class="layui-form-item">
                <label class="layui-form-label"><i
                        style="color: #f00; margin-right: 5px">*</i>活动标题</label>
                <div class="layui-input-block">
                    <input type="text" name="activitytitle" id="activitytitle" lay-verify="title" autocomplete="off"
                           placeholder="请输入标题"
                           class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label"><i
                        style="color: #f00; margin-right: 5px">*</i>活动类型</label>
                <div class="layui-input-block">
                    <select name="activitytype" lay-verify="required" id="activity_type">
                        <option value=""></option>
                        <option value="1">邀请经纪人活动</option>
                        <option value="2">推荐客户活动</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label"><i style="color: #f00; margin-right: 5px">*</i>开始时间</label>
                    <div class="layui-input-block">
                        <input type="text" name="begintime" id="date1" autocomplete="off"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label"><i style="color: #f00; margin-right: 5px">*</i>结束时间</label>
                    <div class="layui-input-block">
                        <input type="text" name="endtime" id="date2" autocomplete="off"
                               class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"><i
                        style="color: #f00; margin-right: 5px">*</i>上传封面图</label>
                <div class="layui-input-block">
                    <div class="layui-upload-drag" id="coverImg">
                        <i class="layui-icon layui-icon-upload-drag"></i>
                        <p>点击上传，或将文件拖拽到此处</p>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"><i
                        style="color: #f00; margin-right: 5px">*</i>活动详情</label>
                <div class="layui-input-block">
                    <textarea id="activity_context" class="layui-textarea" name="activity_context" th:text="${detail}"></textarea>
                </div>
            </div>
            <div class="layui-form-item layui-hide">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit="" lay-filter="addActivitySubmit" id="addActivitySubmit">立即提交
                    </button>
                    <button class="layui-btn" lay-submit="" lay-filter="editActivitySubmit" id="editActivitySubmit">立即修改</button>
                </div>
            </div>
        </form>
    </div>
</div>
</body>
<script>
    KindEditor.ready(function (K) {
        window.editor = K.create('#activity_context', {
            items: ['source', '|', 'undo', 'redo', '|', 'preview', 'print', 'template', 'code', 'cut', 'copy', 'paste',
                'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright',
                'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript',
                'superscript', 'clearhtml', 'quickformat', 'selectall', '|', 'fullscreen', '/',
                'formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold',
                'italic', 'underline', 'strikethrough', 'lineheight', 'removeformat', '|', 'image', 'multiimage',
                'flash', 'media', 'insertfile', 'table', 'hr', 'emoticons', 'baidumap', 'pagebreak',
                'anchor', 'link', 'unlink', '|', 'about'],
            allowFileManager: true,
            filePostName: 'file',//跟服务端参数对应
            uploadJson: "/kindeditor/fileUpload",
            fileManagerJson: "/kindeditor/fileManager",//指定浏览远程图片的服务器端程序
            minHeight: 500,
            afterCreate : function() {
                let self = this;
                K.ctrl(document, 13, function() {
                    self.sync();
                    document.forms['activity_form'].submit();
                });
                K.ctrl(self.edit.doc, 13, function() {
                    self.sync();
                    document.forms['activity_form'].submit();
                });
            },
            afterUpload: function (url) {

            }
        });
        prettyPrint();
        layui.use(['layer', 'form', 'laydate', 'upload', 'table'], function () {
            let user = layui.data('user');
            let layer = layui.layer, form = layui.form, laydate = layui.laydate, upload = layui.upload, $ = layui.$;
            let filename = "";
            laydate.render({
                elem: '#date1',
                type: 'datetime',
                format: 'yyyy-MM-dd H:m:s'
            });
            laydate.render({
                elem: '#date2',
                type: 'datetime',
                format: 'yyyy-MM-dd H:m:s'
            });



            let uploadInst = upload.render({
                elem: '#coverImg'
                , url: '/upload/'
                , before: function (obj) {
                    //预读本地文件示例，不支持ie8
                    obj.preview(function (index, file, result) {
                        $('#coverImg').empty().append("<img src='" + result + "' width='300px' height='300px'>"); //图片链接（base64）
                    });
                }, done: function (res) {
                    //如果上传失败
                    if (res.code > 0) {
                        return layer.msg('上传失败');
                    }
                    filename = res.data.filename;
                    //上传成功
                }, error: function () {
                    //演示失败状态，并实现重传
                    let demoText = $('#demoText');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function () {
                        uploadInst.upload();
                    });
                }
            });

            form.verify({
                activity_context: function (value) {
                    editor.sync();
                }
            });

            //监听提交
            form.on('submit(addActivitySubmit)', function (data) {
                data.field.activity_context = editor.html();
                data.field.coverImg = filename;
                data.field.login_name = user.user_id;
                $.post('addActivity',data.field,function (resp) {
                    if(resp.code == "0"){
                        layer.msg(resp.msg);
                        let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layui.table.reload('activity_table'); //重载表格
                        parent.layer.close(index); //再执行关闭
                    }else{
                        layer.msg(resp.msg);
                    }
                })
                return false;
            });
            function GetQueryString(name) {
                var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
                var r = window.location.search.substr(1).match(reg);
                if (r != null) return unescape(r[2]);
                return null;
            }
            //监听修改
            form.on('submit(editActivitySubmit)',function (data) {
                let field  = data.field;
                field.activity_context = editor.html();
                if(filename != ""){
                    field.coverImg = filename
                }else{
                    field.coverImg = GetQueryString("coverImg");
                }

                field.id = GetQueryString('id');
                $.post('editActivity',field,function (resp) {
                    if(resp.code == "0"){
                        layer.msg(resp.msg);
                        parent.layui.table.reload('activity_table'); //重载表格
                        parent.layer.close(index); //再执行关闭
                    }else{
                        layer.msg(resp.msg);
                    }
                })
                return false;
            })

        });
    });



</script>
</html>